package com.ybkj.o2o.zhuanxian.service.impl;

import com.ybkj.o2o.zhuanxian.common.CompanyOrderVo;
import com.ybkj.o2o.zhuanxian.common.DriverOrderDetailVo;
import com.ybkj.o2o.zhuanxian.common.DriverOrderVo;
import com.ybkj.o2o.zhuanxian.common.MemberOrderVo;
import com.ybkj.o2o.zhuanxian.common.OrderFromSourceVo;
import com.ybkj.o2o.zhuanxian.common.PageVo;
import com.ybkj.o2o.zhuanxian.common.SerialNumber;
import com.ybkj.o2o.zhuanxian.common.chart.ChartDay;
import com.ybkj.o2o.zhuanxian.common.chart.ChartMonth;
import com.ybkj.o2o.zhuanxian.common.chart.ChartYear;
import com.ybkj.o2o.zhuanxian.common.chart.HourChart;
import com.ybkj.o2o.zhuanxian.entity.Order;
import com.ybkj.o2o.zhuanxian.mapper.OrderMapper;
import com.ybkj.o2o.zhuanxian.service.OrderService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DefaultOrderService implements OrderService {

  @Autowired
  private OrderMapper orderMapper;
  @Autowired
  private SerialNumber serialNumber;

  @Override
  public void insertCycleRecord(Map<String, Object> map) {
    orderMapper.insertCycleRecord(map);
  }

  @Override
  public List<Map<String,Object>> redPacketCycle(Date orderDate, String cycle) {
    return orderMapper.redPacketCycle(orderDate, cycle);
  }

  @Override
  public List<Order> cycleOrder(Date startDate, Date endDate) {
    return orderMapper.cycleOrder(startDate, endDate);
  }

  @Override
  public int insert(Order order) {
    Assert.notNull(order.getAppkey());
    // 生产订单号
    DateFormat format = new SimpleDateFormat("yyMMdd");
    Date date = new Date();
    String datestr = format.format(date);
    order.setOrderNo("CJZX" + datestr + serialNumber.getSeqNo(order.getAppkey()));
    order.setCreated(date);
    order.setUpdated(date);
    order.setStatus(0);
    order.setVersion(1L);
    order.setLasturge(date);
    order.setIsClosed(false);
    return orderMapper.insert(order);
  }

  @Override
  public int update(Order order) {
    order.setUpdated(new Date());
    return orderMapper.update(order);
  }

  @Override
  public Order findByIdAndAppKey(Long orderId, String appKey) {
    return orderMapper.findByIdAndAppKey(orderId, appKey);
  }

  @Override
  public int updateIngoreNull(Order order) {
    order.setUpdated(new Date());
    return orderMapper.updateIngoreNull(order);
  }

  @Override
  public List<Order> findOrderByEmployId(Long employId, Double money, String appKey,
      Long startTime, Long endTime) {
    return orderMapper.findOrderByEmployId(employId, money, appKey, startTime, endTime);
  }

  @Override
  public List<Order> findByParam(Date start, Date end, Long companyId, String companyName,
      String orderNo, Long passengerId, String passengerName, String passengerPhone, Long employId,
      String employName, String employPhone, String carNumber, String orderby,
      List<String> allorderType, List<Integer> allstatus, Integer rows, Integer offset,
      Boolean treatment, Date startFinish, Date endFinish, Long lineId, String appKey, Long[] linesId) {
    List<Long> lineIdList = null;
    if(null != linesId && linesId.length > 0){
      lineIdList = new ArrayList<>();
      for(int i = 0;i < linesId.length;i++){
        lineIdList.add(linesId[i]);
      }
    }
    return orderMapper.findByParam(start, end, companyId, StringUtils.isNotBlank(companyName) ? "%"
        + companyName + "%" : null, StringUtils.isNotBlank(orderNo) ? "%" + orderNo + "%" : null,
        passengerId, StringUtils.isNotBlank(passengerName) ? "%" + passengerName + "%" : null,
        StringUtils.isNotBlank(passengerPhone) ? "%" + passengerPhone + "%" : null, employId,
        StringUtils.isNotBlank(employName) ? "%" + employName + "%" : null,
        StringUtils.isNotBlank(employPhone) ? "%" + employPhone + "%" : null,
        StringUtils.isNotBlank(carNumber) ? "%" + carNumber + "%" : null,
        StringUtils.isNotBlank(orderby) ? "%" + orderby + "%" : null,
        !CollectionUtils.isEmpty(allorderType) ? allorderType : null,
        !CollectionUtils.isEmpty(allstatus) ? allstatus : null, rows, offset, treatment,
        startFinish, endFinish, lineId, appKey, lineIdList);
  }

  @Override
  public Long countByParam(Date start, Date end, Long companyId, String companyName,
      String orderNo, Long passengerId, String passengerName, String passengerPhone, Long employId,
      String employName, String employPhone, String carNumber, String orderby,
      List<String> allorderType, List<Integer> allstatus, Boolean treatment, Date startFinish,
      Date endFinish, Long lineId, String appKey, Long[] linesId) {
    List<Long> lineIdList = new ArrayList<>();
    if(null != linesId) {
      for(int i = 0;i < linesId.length;i++){
        lineIdList.add(linesId[i]);
      }

    }
    return orderMapper.countByParam(start, end, companyId,
        StringUtils.isNotBlank(companyName) ? "%" + companyName + "%" : null, StringUtils
            .isNotBlank(orderNo) ? "%" + orderNo + "%" : null, passengerId, StringUtils
            .isNotBlank(passengerName) ? "%" + passengerName + "%" : null, StringUtils
            .isNotBlank(passengerPhone) ? "%" + passengerPhone + "%" : null, employId, StringUtils
            .isNotBlank(employName) ? "%" + employName + "%" : null, StringUtils
            .isNotBlank(employPhone) ? "%" + employPhone + "%" : null, StringUtils
            .isNotBlank(carNumber) ? "%" + carNumber + "%" : null,
        StringUtils.isNotBlank(orderby) ? "%" + orderby + "%" : null,
        allorderType != null ? allorderType : null, allstatus != null ? allstatus : null,
        treatment, startFinish, endFinish, lineId, appKey, lineIdList);
  }

  @Override
  public Long countFinishByPassenger(Long passengerId, Date startTime, Date endTime, String appKey,
      List<String> channelNames) {

    return orderMapper.countFinishByPassenger(passengerId, startTime, endTime, appKey,
        CollectionUtils.isEmpty(channelNames) ? null : channelNames);

  }

  @Override
  public int addmore(Order order) {
    Assert.notNull(order.getAppkey());
    // 生产订单号
    DateFormat format = new SimpleDateFormat("yyMMdd");
    String date = format.format(new Date());
    order.setOrderNo("CJZX" + date + serialNumber.getSeqNo(order.getAppkey()));
    Date date2 = new Date();
    order.setCreated(date2);
    order.setUpdated(date2);
    order.setLasturge(date2);
    order.setStatus(2);
    order.setVersion(1L);
    order.setReview(false);
    order.setTreatment(false);
    return orderMapper.insert(order);
  }

  @Override
  public long countForToday(String appKey) {
    Assert.notNull(appKey);

    Calendar calendar = Calendar.getInstance();
    calendar = DateUtils.truncate(calendar, Calendar.DAY_OF_MONTH);
    Date day1 = calendar.getTime();
    Date day2 = DateUtils.addDays(calendar.getTime(), 1);
    return orderMapper.countForToday(day1, day2, appKey);
  }

  @Override
  public List<Order> findMyFinishOrder(Long passengerId, String appKey) {
    return orderMapper.findMyFinishOrder(passengerId, appKey);
  }

  @Override
  public ChartYear dataForYear(Integer year, Long companyId, String appKey) {
    ChartYear chartYear = new ChartYear();
    BigDecimal total = new BigDecimal(0);
    long destroy = 0;
    long finish = 0;
    try {

      finish =
          orderMapper.finishOrderCount(DateUtils.parseDate(year + "-01-01", "yyyy-MM-dd"),
              DateUtils.parseDate((year + 1) + "-01-01", "yyyy-MM-dd"), companyId, appKey);
      destroy =
          orderMapper.destroyOrderCount(DateUtils.parseDate(year + "-01-01", "yyyy-MM-dd"),
              DateUtils.parseDate((year + 1) + "-01-01", "yyyy-MM-dd"), companyId, appKey);
      List<Integer> status = new ArrayList<Integer>();
      status.add(5);
      List<Order> list =
          orderMapper.findByParam(null, null, companyId, null, null, null, null, null, null, null,
              null, null, null, null, status, null, null, null,
              DateUtils.parseDate(year + "-01-01", "yyyy-MM-dd"),
              DateUtils.parseDate(year + "-12-31", "yyyy-MM-dd"), null, appKey,null);

      for (Order order : list) {
        total = total.add(new BigDecimal(order.getRealPay() != null ? order.getRealPay() : 0));
      }

    } catch (ParseException ex) {

      ex.printStackTrace();
    }


    chartYear.setTotalCancelNum(destroy);
    chartYear.setTotalSuccessNum(finish);
    chartYear.setTotalPrice(total);
    chartYear.setYear(year);
    List<ChartMonth> months = new ArrayList<ChartMonth>();
    for (int i = 0; i < 12; i++) {
      months.add(this.dataForMonth(year, i, companyId, appKey));
    }
    chartYear.setMonths(months);

    return chartYear;
  }

  @Override
  public ChartMonth dataForMonth(Integer year, Integer month, Long companyId, String appKey) {
    ChartMonth chartMonth = new ChartMonth();
    BigDecimal total = new BigDecimal(0);
    long finish = 0;
    long destroy = 0;
    String end = "";
    if (month == 11) {
      end = (year + 1) + "-01-01";
    } else {
      end = year + "-" + (month + 2 > 9 ? month + 2 : "0" + (month + 2)) + "-01";
    }

    try {
      finish =
          orderMapper
              .finishOrderCount(
                  DateUtils.parseDate(year + "-"
                      + ((month + 1) > 9 ? (month + 1) : "0" + (month + 1)) + "-01", "yyyy-MM-dd"),
                  DateUtils.parseDate(end, "yyyy-MM-dd"), companyId, appKey);
      destroy =
          orderMapper
              .destroyOrderCount(
                  DateUtils.parseDate(year + "-"
                      + ((month + 1 > 9) ? (month + 1) : "0" + (month + 1)) + "-01", "yyyy-MM-dd"),
                  DateUtils.parseDate(end, "yyyy-MM-dd"), companyId, appKey);
      List<Integer> status = new ArrayList<Integer>();
      status.add(5);
      List<Order> list =
          orderMapper.findByParam(
              null,
              null,
              companyId,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              status,
              null,
              null,
              null,
              DateUtils.parseDate(year + "-" + ((month + 1) > 9 ? (month + 1) : "0" + (month + 1))
                  + "-01", "yyyy-MM-dd"), DateUtils.parseDate(end, "yyyy-MM-dd"), null, appKey,null);
      for (Order order : list) {
        total =
            total.add(order.getRealPay() != null ? new BigDecimal(order.getRealPay())
                : BigDecimal.ZERO);
      }
      chartMonth.setMonth(month);
      chartMonth.setTotalCancelNum(destroy);
      chartMonth.setTotalSuccessNum(finish);
      chartMonth.setTotalPrice(total);

    } catch (ParseException ex) {

      ex.printStackTrace();
    }

    return chartMonth;
  }

  @Override
  public ChartDay dataForChartDay(Integer year, Integer month, int day, Long companyId,
      String appKey) {
    ChartDay chartDay = new ChartDay();
    BigDecimal total = new BigDecimal(0);
    long finish = 0;
    long destroy = 0;
    String end = "";
    if ((month + 1) == 12 && day == 31) {
      end = (year + 1) + "-01" + "-01 00:00:00";
    } else if (day == 31) {
      end = year + "-" + ((month + 2 > 9 ? month + 2 : "0" + (month + 2))) + "-01 00:00:00";
    } else {
      end =
          year
              + "-"
              + ((month + 1 > 9 ? month + 1 : "0" + (month + 1)) + "-"
                  + ((day + 1) > 9 ? day + 1 : "0" + (day + 1)) + " 00:00:00");
    }

    try {
      finish =
          orderMapper.finishOrderCount(
              DateUtils.parseDate(year
                  + "-"
                  + ((month + 1 > 9 ? month + 1 : "0" + (month + 1)) + "-" + (day > 9 ? day : "0"
                      + day)) + " 00:00:00", "yyyy-MM-dd HH:mm:ss"),
              DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), companyId, appKey);
      destroy =
          orderMapper.destroyOrderCount(
              DateUtils.parseDate(year
                  + "-"
                  + ((month + 1 > 9 ? month + 1 : "0" + (month + 1)) + "-" + (day > 9 ? day : "0"
                      + day)) + " 00:00:00", "yyyy-MM-dd HH:mm:ss"),
              DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), companyId, appKey);
      List<Integer> status = new ArrayList<Integer>();
      status.add(5);
      List<Order> list =
          orderMapper.findByParam(
              null,
              null,
              companyId,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              null,
              status,
              null,
              null,
              null,
              DateUtils.parseDate(year + "-" + (month + 1 > 9 ? month + 1 : "0" + (month + 1))
                  + "-" + (day > 9 ? day : "0" + day) + " 00:00:00", "yyyy-MM-dd HH:mm:ss"),
              DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), null, appKey, null);
      for (Order order : list) {
        total =
            total.add(order.getRealPay() != null ? new BigDecimal(order.getRealPay())
                : BigDecimal.ZERO);
      }

    } catch (ParseException ex) {
      ex.printStackTrace();
    }


    chartDay.setDay(day);
    chartDay.setTotalCancelNum(destroy);
    chartDay.setTotalSuccessNum(finish);
    chartDay.setTotalPrice(total);
    return chartDay;
  }

  @Override
  public HourChart dataForhour(Integer year, Integer month, Integer day, int hour, Long companyId,
      String appKey) {
    HourChart hourChart = new HourChart();
    BigDecimal total = new BigDecimal(0);
    long finish = 0;
    long destroy = 0;
    String end = "";
    StringBuilder endSb = new StringBuilder();
    if ((month + 1) == 12 && day == 31 && hour == 23) {
      end = endSb.append((year + 1)).append("-01").append("-01 00:00:00").toString();
      endSb.delete(0, endSb.length());
    } else if (day == 31 && hour == 23) {
      end =
          endSb.append(year).append("-").append((month + 2 > 9 ? month + 2 : "0" + (month + 2)))
              .append("-01 00:00:00").toString();
      endSb.delete(0, endSb.length());
    } else if (hour == 23) {
      end =
          endSb.append(year).append("-").append((month + 1 > 9 ? month + 1 : "0" + (month + 1)))
              .append("-").append((day + 1) > 9 ? day + 1 : "0" + (day + 1)).append(" 00:00:00")
              .toString();
      endSb.delete(0, endSb.length());
    } else {
      end =
          endSb.append(year).append("-").append((month + 1 > 9 ? month + 1 : "0"))
              .append((month + 1)).append("-").append(((day) > 9 ? day : ("0" + (day))))
              .append(" ").append(hour + 1 > 9 ? "0" + (hour + 1) : hour + 1).append(":00:00")
              .toString();
      endSb.delete(0, endSb.length());
    }

    try {
      finish =
          orderMapper.finishOrderCount(
              DateUtils.parseDate(
                  endSb.append(year).append("-").append((month + 1 > 9 ? month + 1 : "0"))
                      .append((month + 1)).append("-").append((day > 9 ? day : "0" + day))
                      .append(" ").append((hour > 9 ? hour : ("0" + hour))).append(":00:00")
                      .toString(), "yyyy-MM-dd HH:mm:ss"),
              DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), companyId, appKey);
      endSb.delete(0, endSb.length());
      destroy =
          orderMapper.destroyOrderCount(
              DateUtils.parseDate(
                  endSb.append(year).append("-").append((month + 1 > 9 ? month + 1 : "0"))
                      .append((month + 1)).append("-").append((day > 9 ? day : "0" + day))
                      .append(" ").append((hour > 9 ? hour : ("0" + hour))).append(":00:00")
                      .toString(), "yyyy-MM-dd HH:mm:ss"),
              DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), companyId, appKey);
      List<Integer> status = new ArrayList<Integer>();
      status.add(5);
      endSb.delete(0, endSb.length());
      List<Order> list =
          orderMapper.findByParam(null, null, companyId, null, null, null, null, null, null, null,
              null, null, null, null, status, null, null, null, DateUtils.parseDate(
                  endSb.append(year).append("-")
                      .append((month + 1 > 9 ? month + 1 : "0" + (month + 1))).append("-")
                      .append((day > 9 ? day : "0" + day)).append(" ")
                      .append((hour > 9 ? hour : ("0" + hour))).append(":00:00").toString(),
                  "yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(end, "yyyy-MM-dd HH:mm:ss"), null,
              appKey, null);
      for (Order order : list) {
        total =
            total.add(order.getRealPay() != null ? new BigDecimal(order.getRealPay())
                : BigDecimal.ZERO);
      }
      hourChart.setHour(hour);
      hourChart.setTotalCancel(destroy);
      hourChart.setTotalSuccess(finish);
      hourChart.setTotalPrice(total);
    } catch (ParseException ex) {

      ex.printStackTrace();
    }


    return hourChart;
  }

  @Override
  public Order findByDriverAndFinishTime(Long employId, Date start, Date end, String appKey) {
    return orderMapper.selectFinishedByDriverWithDate(employId, start, end, null, null);
  }

  @Override
  public List<Order> findMonthOrder(Integer year, Integer month, String appKey) {
    Calendar calendar = Calendar.getInstance();

    Date start = null;
    Date end = null;

    if (null == year) {
      year = calendar.get(Calendar.YEAR);
    }
    if (null == month) {
      month = calendar.get(Calendar.MONTH);
    }
    try {
      start =
          DateUtils.parseDate(year + "-" + (month > 9 ? month : ("0" + month)) + "-01",
              "yyyy-MM-dd");// 2015-01-01
      if (month != 12) {
        end =
            DateUtils.parseDate(year + "-" + ((month + 1) > 9 ? (month + 1) : "0" + (month + 1))
                + "-01", "yyyy-MM-dd");
      } else {
        end = DateUtils.parseDate((year + 1) + "-" + "01" + "-01", "yyyy-MM-dd");// 2015-01-01
      }
    } catch (ParseException ex) {
      ex.printStackTrace();
    }
    List<Order> list = orderMapper.selectMonthOrder(start, end);
    return list;
  }

  @Override
  public PageVo<MemberOrderVo> findMemberOrder(Date startTime, Date endTime, Long companyId,
      String appKey) {
    List<MemberOrderVo> list = orderMapper.selectMemberOrder(startTime, endTime, companyId, appKey);
    PageVo<MemberOrderVo> pageVo = new PageVo<MemberOrderVo>();
    pageVo.setContent(list);
    pageVo.setTotalElements(list.size());

    return pageVo;
  }

  @Override
  public PageVo<OrderFromSourceVo> orderFromSource(Date date1, Date date2, Long companyId,
      String appKey) {
    PageVo<OrderFromSourceVo> orderFromSources = new PageVo<OrderFromSourceVo>();
    List<OrderFromSourceVo> orderFromSourceList = new ArrayList<OrderFromSourceVo>();
    BigDecimal amount = BigDecimal.ZERO;
    BigDecimal aggregateAmount = BigDecimal.ZERO;
    List<String> fromSource = orderMapper.queryAllFromSource(companyId, appKey);
    for (String str : fromSource) {
      Map<String, Object> map =
          orderMapper.findByFromSource(date1, date2, companyId, "%" + str + "%", appKey);
      OrderFromSourceVo sourceVo = new OrderFromSourceVo();
      sourceVo.setFromSource(str);
      sourceVo
          .setMoney(map == null ? new BigDecimal(0) : new BigDecimal((Double) map.get("money")));
      sourceVo.setAmount(map == null ? 0 : (long) map.get("count"));
      /*
       * if(str.contains("后台")||str.contains("系统")||str.contains("微信经理")){ List<Map<String,Object>>
       * list = orderMapper.findDetailByFromSource(date1,date2,companyId, "%"+str+"%", appKey);
       * List<MemberOrderVo> memberOrderVoList=new ArrayList<MemberOrderVo>();
       * for(Map<String,Object> map1 : list){ MemberOrderVo vo = new MemberOrderVo();
       * vo.setId((long)map1.get("id")); vo.setName((String)map1.get("name"));
       * vo.setPhone((String)map1.get("phone")); vo.setMoney(new
       * BigDecimal((Double)map1.get("money"))); vo.setFinishNums((long)map1.get("finishNums"));
       * memberOrderVoList.add(vo); } sourceVo.setMemberOrderVoList(memberOrderVoList); }
       */
      orderFromSourceList.add(sourceVo);
      amount = amount.add(map == null ? BigDecimal.ZERO : new BigDecimal(map.get("count") + ""));
      aggregateAmount =
          aggregateAmount
              .add(map == null ? BigDecimal.ZERO : new BigDecimal(map.get("money") + ""));
    }

    orderFromSources.setTotal(amount);
    orderFromSources.setContent(orderFromSourceList);
    orderFromSources.setTotalElements(orderFromSourceList.size());
    orderFromSources.setAllTotal(aggregateAmount);

    return orderFromSources;
  }

  @Override
  public PageVo<DriverOrderVo> driverAchievement(Date date1, Date date2, Long empCompanyId,
      String appKey) {
    List<DriverOrderVo> list =
        orderMapper.queryAllDriverAchievement(date1, date2, empCompanyId, appKey);
    PageVo<DriverOrderVo> vo = new PageVo<DriverOrderVo>();
    vo.setContent(list);
    return vo;
  }

  @Override
  public PageVo<CompanyOrderVo> companyOrder(Date date1, Date date2, String appKey) {
    PageVo<CompanyOrderVo> companyOrders = new PageVo<CompanyOrderVo>();
    List<CompanyOrderVo> finishList = new ArrayList<CompanyOrderVo>();
    List<CompanyOrderVo> cancelList = new ArrayList<CompanyOrderVo>();

    List<CompanyOrderVo> orders = new ArrayList<CompanyOrderVo>();
    List<String> companys = new ArrayList<String>();
    companys = orderMapper.queryAllCompany(appKey);
    finishList = orderMapper.companyOrders(date1, date2, appKey);
    cancelList = orderMapper.companyCancelNums(date1, date2, appKey);
    BigDecimal finishcount = BigDecimal.ZERO;
    BigDecimal cancelcount = BigDecimal.ZERO;
    BigDecimal money = BigDecimal.ZERO;
    for (String company : companys) {
      CompanyOrderVo vo = new CompanyOrderVo();
      vo.setCompanyName(company);
      vo.setFinishNums(0);
      vo.setCancelNums(0);

      for (CompanyOrderVo companyVo : finishList) {
        if (company.equals(companyVo.getCompanyName())) {
          vo.setFinishNums(companyVo.getFinishNums());
          vo.setMoney(companyVo.getMoney());
          break;
        }
      }
      for (CompanyOrderVo companyVo : cancelList) {
        if (company.equals(companyVo.getCompanyName())) {
          vo.setCancelNums(companyVo.getCancelNums());
          break;
        }
      }

      finishcount = finishcount.add(new BigDecimal(vo.getFinishNums()));
      cancelcount = cancelcount.add(new BigDecimal(vo.getCancelNums()));
      money = money.add(vo.getMoney());
      orders.add(vo);
    }
    companyOrders.setContent(orders);
    companyOrders.setAllTotal(finishcount);
    companyOrders.setSumMoney(money);
    companyOrders.setTotal(cancelcount);

    return companyOrders;
  }

  @Override
  public PageVo<DriverOrderDetailVo> driverOrderDetailVo(Long driverId, Date start, Date end,
      Integer page, Integer rows, String appKey) {
    Integer offset = null;
    if (page != null && rows != null) {
      offset = (page - 1) * rows;
    }
    List<DriverOrderDetailVo> list =
        orderMapper.queryDetailByDriverId(driverId, start, end, offset, rows, appKey);
    PageVo<DriverOrderDetailVo> pageVo = new PageVo<DriverOrderDetailVo>();
    pageVo.setTotalElements(list.size());
    pageVo.setContent(list);
    return pageVo;
  }


  @Override
  public List<Order> queryFinishOrderByEmployId(Long employId, Long start, Long end, Integer page,
      Integer rows, String appKey) {
    Date sdate = null;
    if (start != null) {
      sdate = new Date(start);
    }
    Date edate = null;
    if (end != null) {
      edate = new Date(end);
    }
    Integer offset = (page - 1) * rows;
    return orderMapper.queryFinishOrderByEmployId(employId, sdate, edate, appKey, offset, rows);
  }

  @Override
  public Long countFinishOrderByEmployId(Long employId, Long start, Long end, String appKey) {
    Date sdate = null;
    if (start != null) {
      sdate = new Date(start);
    }
    Date edate = null;
    if (end != null) {
      edate = new Date(end);
    }
    return orderMapper.countFinishOrderByEmployId(employId, sdate, edate, appKey);
  }

  @Override
  public Long countByPassengerId(Long passengerId, String appKey) {
    List<String> channelNameList = new ArrayList<String>();
    channelNameList.add("APP预约");
    channelNameList.add("微信预约");
    channelNameList.add("网站预约");

    Long count = 0L;
    count = orderMapper.countByPassengerId(passengerId, channelNameList, appKey);

    return count;
  }

  @Override
  public Long countRunning(Long passengerId, String appKey) {


    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date startDate = new Date();

    startDate = calendar.getTime();

    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR, 23);
    calendar.add(Calendar.MINUTE, 59);
    calendar.add(Calendar.SECOND, 59);
    Date endDate = new Date();
    endDate = calendar.getTime();

    List<String> channelNameList = new ArrayList<String>();
    channelNameList.add("APP预约");
    channelNameList.add("微信预约");
    channelNameList.add("网站预约");

    Long count = 0L;
    count = orderMapper.countRunning(passengerId, startDate, endDate, channelNameList, appKey);
    return count;
  }

  @Override
  public List<Order> selectActivedByEmploy(Long employId, String appKey) {
    return orderMapper.selectActivedByEmploy(employId, appKey);
  }

  @Override
  public List<Order> selectNewOrder(Long companyId, String appKey) {
    return orderMapper.selectNewOrder(companyId, appKey);
  }

  @Override
  public List<Order> findRunOrderByEmployId(Long employId, Boolean treatment, String appKey) {
    return orderMapper.findRunOrderByEmployId(employId, treatment, appKey);
  }

  @Override
  public Long queryRunOrder(Long passengerId, String appKey) {
    return orderMapper.queryRunOrder(passengerId, appKey);
  }

  @Override
  public List<Order> findHastenOrder(String appKey) {

    return orderMapper.findHastenOrder(appKey);
  }

  @Override
  public List<Order> findUnexecutedByEmployId(Long employId, String appKey) {

    return orderMapper.findUnexecutedByEmployId(employId, appKey);
  }

  @Override
  public List<Order> findOrderByPassenger(Long passengerId, Integer offset, Integer rows,
      String appKey) {

    return orderMapper.findOrderByPassenger(passengerId, offset, rows, appKey);
  }

  @Override
  public Long countOrderByPassengerId(Long passengerId, String appKey) {
    return orderMapper.countOrderByPassengerId(passengerId, appKey);
  }

  @Override
  public List<Order> findByWork(Long employId, List<Integer> statusList, String appKey) {
    return orderMapper.findByWork(employId, statusList, appKey);
  }

  @Override
  public List<Order> findRunOrderByLineId(Long lineId, String appKey) {
    return orderMapper.findRunOrderByLineId(lineId, appKey);
  }

  @Override
  public List<Order> findRunOrderByCarId(Long carId, String appKey) {
    return orderMapper.findRunOrderByCarId(carId, appKey);
  }

  @Override
  public List<Order> findJiedanOrderByEmployId(Long employId, String appKey) {
    return orderMapper.findJiedanOrderByEmployId(employId, appKey);
  }

  @Transactional(rollbackFor = Exception.class)
  public int prepare(Long orderId,String appKey) {
    Order order = orderMapper.selectPrepare(orderId,appKey);
    return orderMapper.updateFinishTime(order.getId(),appKey);
  }

  @Override
  public Long driverOrderDetailCount(Long employId, Date startDate, Date endDate, String appKey) {
    return orderMapper.driverOrderDetailCount(employId,startDate,endDate,appKey);
  }

  @Override
  public int updateAnonymousNumber(Long orderId, String appKey, String anonymousNumber) {
    return orderMapper.updateAnonymousNumber(orderId,appKey,anonymousNumber);
  }

}
